/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import java.sql.SQLException;
import java.util.ArrayList;
import util.Constante;

/**
 *
 * @author gabrielbmoro
 */
public class Aluno extends Usuario {

    public Aluno() {
        super.table = "aluno";
    }

    public Aluno(String nome, String email) {
        super.nome = nome;
        super.email = email;
        super.table = "aluno";
    }

    public Aluno(String nome) {
        this.nome = nome;
        super.table = "aluno";
    }

    @Override
    public ArrayList<String> acessarPerfil(int id) {
        String sql = "select nome,email from " + super.table + " where idaluno=" + id + ";";
        try {
            this.conecta = ConectaBD.getInstance();
            super.statement = this.conecta.getConnection().createStatement();
            super.resultSet = super.statement.executeQuery(sql);
            ArrayList<String> resultado = new ArrayList<>();
            while (super.resultSet.next()) {
                resultado.add(super.resultSet.getString(1));
                resultado.add(super.resultSet.getString(2));
                resultado.add("Universidade Federal do Pampa");
            }
            super.resultSet.close();
            super.statement.close();
            return resultado;
        } catch (SQLException sqlErro) {
            sqlErro.printStackTrace();
        }
        return null;
    }

    private void setarIdDeOrientacao(Tcc tcc, int idaluno) {
        if (tcc != null) {
            int id = tcc.recuperarIdTcc(tcc.getTitulo(), tcc.getProposta());
            try {
                String sql = "update " + super.table + " set tcc_idtcc=? where idaluno=" + idaluno + ";";
                super.conecta = ConectaBD.getInstance();
                super.preparedStatement = super.conecta.getConnection().prepareStatement(sql);
                super.preparedStatement.setInt(1, id);
                super.preparedStatement.execute();
                super.preparedStatement.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                super.conecta.encerrarConexao();
            }
        }
    }

    public boolean solicitarOrientacao(Tcc tcc, int idaluno) {
        try {
            int idProfessor = tcc.getOrientador().recuperarIdProfessor();

            String sql = "insert into tcc(tema, proposta,orientador_professor,solicitacaoDeOrientacao) values(?,?,?,?);";
            super.conecta = ConectaBD.getInstance();
            super.preparedStatement = super.conecta.getConnection().prepareStatement(sql);
            super.preparedStatement.setString(1, tcc.getTitulo());
            super.preparedStatement.setString(2, tcc.getProposta());
            super.preparedStatement.setInt(3, idProfessor);
            super.preparedStatement.setInt(4, Constante.STATUS_AGUARDANDO);
            super.preparedStatement.execute();
            super.preparedStatement.close();

            setarIdDeOrientacao(tcc, idaluno);
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            super.conecta.encerrarConexao();
        }
        return false;
    }

    public ArrayList<String> restauraSolicitacaoDeOrientacao(int id) {
        String sql = "select tcc_idtcc from " + super.table + " WHERE idaluno=" + id + ";";
        try {
            this.conecta = ConectaBD.getInstance();
            super.statement = this.conecta.getConnection().createStatement();
            super.resultSet = super.statement.executeQuery(sql);
            int idTcc = 0;
            while (super.resultSet.next()) {
                idTcc = super.resultSet.getInt(1);
            }
            Tcc tcc = new Tcc();
            ArrayList<String> resultado = tcc.getDadosDeTcc(idTcc);
            super.resultSet.close();
            super.statement.close();
            return resultado;

        } catch (SQLException sqlErro) {
            sqlErro.printStackTrace();
        }
        return null;
    }

    public int recuperaIdAlunoPorTcc(int idTcc) {
        try {
            int idAluno = 0;

            String sql = "select idaluno from " + super.table + " where tcc_idtcc=" + idTcc + ";";

            super.conecta = ConectaBD.getInstance();
            super.statement = super.conecta.getConnection().createStatement();
            super.resultSet = super.statement.executeQuery(sql);

            while (super.resultSet.next()) {
                idAluno = super.resultSet.getInt(1);
            }
            return idAluno;
        } catch (SQLException ex) {

        } finally {

        }
        return 0;
    }

    public int recuperaIdTcc(int idAluno) {
        try {
            String sql = "select tcc_idtcc from " + super.table + " where idaluno=" + idAluno + ";";
            super.conecta = ConectaBD.getInstance();
            super.statement = super.conecta.getConnection().createStatement();
            super.resultSet = super.statement.executeQuery(sql);
            int id = 0;
            while (super.resultSet.next()) {
                id = super.resultSet.getInt(1);
            }
            return id;
        } catch (SQLException ex) {

        } finally {

        }
        return 0;

    }

    public int recuperaIdAlunoPorNome(String nomeAluno) {
        try {
            int idAluno = 0;

            String sql = "select idaluno from " + super.table + " where nome=\"" + nomeAluno + "\";";

            super.conecta = ConectaBD.getInstance();
            super.statement = super.conecta.getConnection().createStatement();
            super.resultSet = super.statement.executeQuery(sql);

            while (super.resultSet.next()) {
                idAluno = super.resultSet.getInt(1);
            }
            return idAluno;
        } catch (SQLException ex) {

        } finally {

        }
        return 0;
    }

}
